using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace eTMS.Controls
{
	/// <summary>
	/// Summary description for GradientPanel.
	/// </summary>
	public class GradientPanel:eTMS.Controls.Panel
	{	
		private Color endgradColor=Color.FromArgb(220,220,230);
		private Color startgradColor=Color.FromArgb(220,230,240);
		private LinearGradientMode gradientMode=LinearGradientMode.Vertical;
		private float gradPercent=0.5f;
        public bool IsBlend = false;
		public LinearGradientMode GradientMode
		{
			get
			{
				return gradientMode;
			}
			set
			{
				gradientMode=value;
				this.Invalidate();
			}
		}
		public float GradientPercent
		{
			get
			{
				return gradPercent;
			}
			set
			{
				gradPercent=value;
				this.Invalidate();
			}
		}
		public Color StartGradientColor{
			get{
				return startgradColor;
			}
			set{
				startgradColor=value;
				this.Invalidate();
			}
		}
		public Color EndGradientColor
		{
			get
			{
				return endgradColor;
			}
			set
			{
				endgradColor=value;		
				this.Invalidate();
			}
		}
		public GradientPanel()
		{		
			this.BackColor=Color.FromArgb(255,255,255);
			this.StartGradientColor=Color.FromArgb(215,229,247);
			this.EndGradientColor=Color.FromArgb(186,211,245);
			this.BorderColor=Color.RoyalBlue;
		}
		private void DrawBackGround(Graphics g){	
			// Create a rectangle
			g.Clear(this.BackColor);
			Rectangle rect = this.ClientRectangle;
			// Create a linear gradient brush
			LinearGradientBrush rgBrush =
				new LinearGradientBrush(
				rect, this.startgradColor,this.endgradColor,gradientMode
			);					// Set signma bell shape
			rgBrush.SetSigmaBellShape(gradPercent, 1.0f);		
			// Fill rectangle again
			g.FillRectangle(rgBrush, rect);
			// Dispose of object
			Pen p=new Pen(this.BorderColor);
			g.DrawRectangle(p,this.ClientRectangle);
			p.Dispose();
            if (IsBlend)
            {
                Blend blend = new Blend();
                float[] factArray = { 0.0f, 0.3f, 0.8f, 1.0f };
                float[] posArray = { 0.0f, 0.4f, 0.6f, 1.0f };
                // Set Factors and Positions properties of Blend
                blend.Factors = factArray;
                blend.Positions = posArray;
                // Set smoothing mode of Graphics object
                g.SmoothingMode = SmoothingMode.AntiAlias;
                // Create path and add a rectangle
                GraphicsPath path = new GraphicsPath();
                Rectangle rc = new Rectangle(rect.X - rect.Width / 2, rect.Y - rect.Height, 2 * rect.Width, 6 * rect.Height);
                path.AddEllipse(rc);
                // Create path gradient brush
                PathGradientBrush pthBrush =
                    new PathGradientBrush(path);
                // Set Blend and FocusScales properties
                pthBrush.Blend = blend;
                pthBrush.FocusScales = new PointF(0.7f, 0.2f);
                Color[] colors = { Color.FromArgb(10, this.startgradColor) };
                // Set CenterColor and SurroundColors properties
                pthBrush.CenterColor = Color.FromArgb(200, Color.White);
                pthBrush.SurroundColors = colors;
                g.FillEllipse(pthBrush, rc);
                pthBrush.Dispose();
            }
	
		}		
		protected override void OnDraw(System.Windows.Forms.PaintEventArgs e)
		{
			base.OnDraw (e);
			DrawBackGround(e.Graphics);
		}

		private int roundPercent=0;
		public int RoundPercent
		{
			get
			{
				return roundPercent;
			}
			set
			{
				roundPercent=value;
			}
		}	
		protected override void OnSizeChanged(EventArgs e)
		{
			base.OnSizeChanged (e);
			Rectangle rc=this.ClientRectangle;
			if(roundPercent<=0)return;
			if(roundPercent>=100)return;
			int R=Math.Min(rc.Width,rc.Height)*roundPercent/100;
			GraphicsPath gph=new GraphicsPath();
			gph.AddArc(0,0,R,R,180,90);
			gph.AddLine(rc.Left+R/2,rc.Top,rc.Left+rc.Width-R/2,rc.Top);
			gph.AddArc(rc.Left+rc.Width-R,rc.Top,R,R,270,90);
			gph.AddLine(rc.Left+rc.Width,rc.Top+R/2,rc.Left+rc.Width,rc.Top+rc.Height-R/2);
			gph.AddArc(rc.Left+rc.Width-R,rc.Top-R+rc.Height,R,R,0,90);
			gph.AddLine(rc.Left+rc.Width-R/2,rc.Top+rc.Height,rc.Left+R/2,rc.Top+rc.Height);
			gph.AddArc(rc.Left,rc.Top-R+rc.Height,R,R,90,90);
			gph.AddLine(rc.Left,rc.Top+rc.Height-R/2,rc.Left,rc.Top+R/2);
			Region rg=new Region(gph);			
			this.Region=rg;			
			this.Invalidate();
		}
	}
}
